package com.ys.nms.module.business.controller.admin.alertRules;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ys.nms.framework.common.pojo.PageParam;
import com.ys.nms.framework.common.pojo.PageResult;
import com.ys.nms.framework.common.pojo.CommonResult;
import com.ys.nms.framework.common.util.object.BeanUtils;
import static com.ys.nms.framework.common.pojo.CommonResult.success;

import com.ys.nms.framework.excel.core.util.ExcelUtils;

import com.ys.nms.framework.apilog.core.annotation.ApiAccessLog;
import static com.ys.nms.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ys.nms.module.business.controller.admin.alertRules.vo.*;
import com.ys.nms.module.business.dal.dataobject.alertRules.AlertRulesDO;
import com.ys.nms.module.business.service.alertRules.AlertRulesService;

@Tag(name = "管理后台 - 报警规则")
@RestController
@RequestMapping("/business/alert-rules")
@Validated
public class AlertRulesController {

    @Resource
    private AlertRulesService alertRulesService;

    @PostMapping("/create")
    @Operation(summary = "创建报警规则")
    public CommonResult<Integer> createAlertRules(@Valid @RequestBody AlertRulesSaveReqVO createReqVO) {
        return success(alertRulesService.createAlertRules(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新报警规则")
    public CommonResult<Boolean> updateAlertRules(@Valid @RequestBody AlertRulesSaveReqVO updateReqVO) {
        alertRulesService.updateAlertRules(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除报警规则")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteAlertRules(@RequestParam("id") Integer id) {
        alertRulesService.deleteAlertRules(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除报警规则")
    public CommonResult<Boolean> deleteAlertRulesList(@RequestParam("ids") List<Integer> ids) {
        alertRulesService.deleteAlertRulesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得报警规则")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AlertRulesRespVO> getAlertRules(@RequestParam("id") Integer id) {
        AlertRulesDO alertRules = alertRulesService.getAlertRules(id);
        return success(BeanUtils.toBean(alertRules, AlertRulesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得报警规则分页")
    public CommonResult<PageResult<AlertRulesRespVO>> getAlertRulesPage(@Valid AlertRulesPageReqVO pageReqVO) {
        PageResult<AlertRulesDO> pageResult = alertRulesService.getAlertRulesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AlertRulesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出报警规则 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAlertRulesExcel(@Valid AlertRulesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AlertRulesDO> list = alertRulesService.getAlertRulesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "报警规则.xls", "数据", AlertRulesRespVO.class,
                        BeanUtils.toBean(list, AlertRulesRespVO.class));
    }

}